package com.funtonow.jqdk.utils;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.text.TextUtils;

import com.funtonow.jqdk.base.SqjAppliaction;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Properties;
import java.util.Random;

/**
 * @author Jiajien
 * @date 2019/6/17
 */
public class PhoneUtils {

    private static String[] telFirst = "134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(",");

    /***
     *
     * Project Name: recruit-helper-util
     * <p>随机生成手机号码
     *
     * @author youqiang.xiong
     * @date 2018年5月23日  下午2:14:17
     * @version v1.0
     * @since
     * @return
     */
    public static String getTelephone() {
        int index = getNum(0, telFirst.length - 1);
        String first = telFirst[index];
        String second = String.valueOf(getNum(1, 888) + 10000).substring(1);
        String thrid = String.valueOf(getNum(1, 9100) + 10000).substring(1);
        String phone = first + second + thrid;
        return phone.substring(0, 3) + "****" + phone.substring(7, phone.length());
    }

    public static int getNum(int start, int end) {
        return (int) (Math.random() * (end - start + 1) + start);
    }

    public static int getMoneyNum(int start, int end) {
        return Math.abs(end - start) / 2;
    }

    public static String getEduMax(int eduMax) {
        if (eduMax > 30000) {
            return eduMax / 10000 + "万";
        } else {
            return eduMax + "";
        }
    }

    /**
     * 检测是否有imsi
     *
     * @return
     */
    public static boolean checkPhoneState() {
        PackageManager pkgManager = SqjAppliaction.getContext().getPackageManager();
        return pkgManager.checkPermission(Manifest.permission.READ_PHONE_STATE,
                SqjAppliaction.getContext().getPackageName()) == PackageManager.PERMISSION_GRANTED;
    }

    /**
     * 检测是否有读权限
     *
     * @return
     */
    public static boolean checkRead() {
        PackageManager pkgManager = SqjAppliaction.getContext().getPackageManager();
        return pkgManager.checkPermission(Manifest.permission.READ_EXTERNAL_STORAGE,
                SqjAppliaction.getContext().getPackageName()) == PackageManager.PERMISSION_GRANTED;
    }

    public static String getRandomString(int length){
        //定义一个字符串（A-Z，a-z，0-9）即62位；
        String str="zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
        //由Random生成随机数
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        //长度为几就循环几次
        for(int i=0; i<length; ++i){
            //产生0-61的数字
            int number=random.nextInt(62);
            //将产生的数字通过length次承载到sb中
            sb.append(str.charAt(number));
        }
        //将承载的字符转换成字符串
        return sb.toString();
    }

    /**
     * 获取手机型号
     *
     * @return 手机型号
     */
    public static String getSystemModel() {
        return android.os.Build.MODEL;
    }

    /**
     * 获取手机厂商
     *
     * @return 手机厂商
     */
    public static String getDeviceBrand() {
        return android.os.Build.BRAND;
    }


    public static final String SYS_EMUI = "sys_emui";
    public static final String SYS_MIUI = "sys_miui";
    public static final String SYS_FLYME = "sys_flyme";
    public static final String SYS_OTHER = "sys_other";
    private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
    private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
    private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";
    private static final String KEY_EMUI_API_LEVEL = "ro.build.hw_emui_api_level";
    private static final String KEY_EMUI_VERSION = "ro.build.version.emui";
    private static final String KEY_EMUI_CONFIG_HW_SYS_VERSION = "ro.confg.hw_systemversion";

    public static String getSystem(){
        String sysType = "";
        if (TextUtils.isEmpty(sysType)) {
            try {
                sysType = SYS_OTHER;
                Properties prop = new Properties();
                prop.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
                if (prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null
                        || prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null
                        || prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null) {
                    sysType = SYS_MIUI;//小米
                } else if (prop.getProperty(KEY_EMUI_API_LEVEL, null) != null
                        || prop.getProperty(KEY_EMUI_VERSION, null) != null
                        || prop.getProperty(KEY_EMUI_CONFIG_HW_SYS_VERSION, null) != null) {
                    sysType = SYS_EMUI;//华为
                } else if (getMeizuFlymeOSFlag().toLowerCase().contains("flyme")) {
                    sysType = SYS_FLYME;//魅族
                }

            } catch (IOException e) {
                e.printStackTrace();
                return sysType;
            }
        }
        return sysType;
    }

    private static String getMeizuFlymeOSFlag() {
        return getSystemProperty("ro.build.display.id", "");
    }

    private static String getSystemProperty(String key, String defaultValue) {
        try {
            Class<?> clz = Class.forName("android.os.SystemProperties");
            Method get = clz.getMethod("get", String.class, String.class);
            return (String)get.invoke(clz, key, defaultValue);
        } catch (Exception e) {
        }
        return defaultValue;
    }


}
